//
//  CustomAlertView.m
//  upnpxdemo
//
//  Created by 赵帅 on 13-9-4.
//  Copyright (c) 2013年 wiimu.inc. All rights reserved.
//

#import "CustomAlertView.h"
#import <QuartzCore/QuartzCore.h>

@interface CustomAlertView()
{

}
//backview
@property (assign) IBOutlet UIView * backView;

//insideView
@property (assign) IBOutlet UIControl * insideView;
@property (assign) IBOutlet UILabel * titleLabel;
@property (assign) IBOutlet UIButton * cancelButton;
@property (assign) IBOutlet UIButton * confirmButton;
@property (assign) NSObject<CustomAlertViewDelegate> * m_handler;

@end

@implementation CustomAlertView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

-(void)setTitle:(NSString *)title message:(NSString *)message confirmButton:(NSString *)confirm cancelButton:(NSString *)cancel delegate:(NSObject<CustomAlertViewDelegate> *)delegate
{
    _m_handler = delegate;
    _titleLabel.text = title?title:@"";
    [_confirmButton setTitle:confirm?confirm:@"" forState:UIControlStateNormal];
    [_cancelButton setTitle:cancel?cancel:@"" forState:UIControlStateNormal];
    
    NSString * str = message?message:@"";
    
    CGSize maxSize = CGSizeMake(230, 60);
    CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:maxSize lineBreakMode:NSLineBreakByTruncatingTail];
    _messageLabel.numberOfLines = 0;
    _messageLabel.text = message?message:@"";
    _messageLabel.frame = CGRectMake(12, 53, 230, size.height);
    
}

-(IBAction)confirmButtonPressed:(id)sender
{
    if (nil != _m_handler
		&& [_m_handler respondsToSelector:@selector(CustomAlertView:selected:)])
	{
        [_m_handler CustomAlertView:self selected:YES];
	}
    
    [self disappearAnimation];
}

-(IBAction)cancelButtonPressed:(id)sender
{
    if (nil != _m_handler
		&& [_m_handler respondsToSelector:@selector(CustomAlertView:selected:)])
	{
        [_m_handler CustomAlertView:self selected:NO];
	}
    
    [self disappearAnimation];
}

-(void)awakeFromNib
{
    if([GlobalInfo sharedInstance].deviceType == IPHONE4_TYPE)
    {
        _backView.frame = CGRectMake(0, 0, 320, 568);
//        _insideView.frame = CGRectMake(_insideView.frame.origin.x, _insideView.frame.origin.y+88, _insideView.frame.size.width, _insideView.frame.size.height);
    }
    
    [self appearAnimation];
}

-(void)appearAnimation
{
    CABasicAnimation *forwardAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    forwardAnimation.duration = 0.2f;
    forwardAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.5f :1.7f :0.6f :0.85f];
    forwardAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
    forwardAnimation.toValue = [NSNumber numberWithFloat:1.0f];
    [_insideView.layer addAnimation:forwardAnimation forKey:@"popin"];
    
    CABasicAnimation *fadein = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadein.toValue = [NSNumber numberWithFloat:0.7];
    fadein.duration = 0.2;
    fadein.fillMode = kCAFillModeForwards;
    fadein.removedOnCompletion = NO;
    fadein.delegate = self;
    [_backView.layer addAnimation:fadein forKey:@"fadein"];
}

-(void)disappearAnimation
{
    CABasicAnimation *fadeout = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeout.toValue = [NSNumber numberWithFloat:0];
    fadeout.duration = 0.2f;
    fadeout.fillMode = kCAFillModeForwards;
    fadeout.removedOnCompletion = NO;
    fadeout.delegate = self;
    [_backView.layer addAnimation:fadeout forKey:@"fadeout"];
    
    CABasicAnimation *reverseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    reverseAnimation.duration = 0.2f;
    reverseAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.4f :0.15f :0.5f :-0.7f];
    reverseAnimation.fromValue = [NSNumber numberWithFloat:1.0f];
    reverseAnimation.toValue = [NSNumber numberWithFloat:0.0f];
    reverseAnimation.delegate = self;
    reverseAnimation.removedOnCompletion = NO;
    reverseAnimation.fillMode = kCAFillModeForwards;
    [_insideView.layer addAnimation:reverseAnimation forKey:@"kLPAnimationKeyPopup"];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    if(flag)
    {
        if(anim == [_backView.layer animationForKey:@"fadein"])
        {
            self.backView.alpha = 0.7;
        }
        else if(anim == [_backView.layer animationForKey:@"fadeout"])
        {
            [self removeFromSuperview];
        }
    }
}

@end
